define(function(require){
	
	var $=require("jq.js");
	var scroll_css=require("/pinzhiV2.0/css/scrollbar.css");
	var ScrollBar=require("pz-scrollbar.js");
	function ShopCart(obj){
		
		var _this_=this;
		
		this.shopCartBox=obj;
		
		this.shopCartContent=$(".shop-cart-con",obj);
		//保存购物车数量和节点
		this.shopCartNode=$(".shop-cart-num",obj);
		this.totalSize=parseInt($(".shop-cart-num",obj).text());
		
		this.shopCartBox.hover(function(){
			var _this=this;
			this.timer=window.setTimeout(function(){
				_this_.show(_this);
			},300);
		},function(){
			var _this=this;
			window.clearTimeout(this.timer);
			_this_.hide();
		});
		//如果购物车数量为0
		if(this.totalSize==0){
			//载入此标签
			this.shopCartContent.html("<p class='shop-cart-null'>您购物车里还没有任何宝贝哟~</p>");
		};
	};
	ShopCart.prototype={
		show:function(thisObj){
			$(".shop-cart-num",thisObj).addClass("hover");
			this.shopCartContent.addClass("shop-cart-con-show");
			//如果数量大于0,就加载外部购物车数据
			//if(this.totalSize!=0){
				this.loadShopCart();
			//};
		},
		hide:function(thisObj){
			$(".shop-cart-num",thisObj).removeClass("hover");
			this.shopCartContent.removeClass("shop-cart-con-show");
		},
		loadShopCart:function(){
				var _this=this;
				this.shopCartContent.html("<p class='shop-cart-reload'><img src='/pinzhiV2.0/images/loading.gif' width='15' height='15' >正在刷新购物车...</p>").load("/pinzhiV2.0/minCart_shop.do?"+Math.random(),function(ret){
					_this.getSizeAndSet(ret);
					var scroll=new ScrollBar($(".J_PZScrollbar").eq(0));
					//初始化checkboxs
					_this.initCheckBox();
				});

		},
		getSizeAndSet:function(ret){
			var size=this.shopCartContent.find("dd").size();
			//更改购物车数值
			this.shopCartNode.text(size);
			//数据成功够，设置滚动条
			this.setScrollbar(size);
		},
		setScrollbar:function(totalSize){
			var h=totalSize<=6?55*totalSize:320;
			$(".scrollbar").eq(0).height(h);
			$(".scrollbar-area").eq(0).height(h);
			$(".scrollbar-track").eq(0).height(h);
			if(this.totalSize<=6){
				$(".scroll-content").eq(0).height(h-1).width(240);
			};
		},
		initCheckBox:function(){
			var _this=this;
			//设置复选框和价格,提交按钮
			var fullBtn=$(".full-select input");
			var productsCheckboxs=$(".shop-product-list input");
			var totalPrice=$(".total-price b");
			this.submitBrn=$(".shop-cart-btn a");
			var l=0;
			fullBtn.click(function(){
				if(this.checked){
					var v=0;
					productsCheckboxs.each(function(){
						this.checked=true;
						v+=Number(this.value);
					});
					totalPrice.text(v);
				}else{
					productsCheckboxs.each(function(){
						this.checked=false;
					});
					totalPrice.text("0.00");
				};
			});
		
			productsCheckboxs.click(function(){
				if(this.checked){
					totalPrice.text(Number(totalPrice.text())+Number(this.value));
				}else{
					totalPrice.text(Number(totalPrice.text())-Number(this.value));
				};
				
				productsCheckboxs.each(function(i){
					l=0;
					if(this.checked){
						l=i;
					}else{
						return false;
					};
					
				});
				
				if(l==productsCheckboxs.size()-1){
					fullBtn.prop("checked",true);
				}else{
					fullBtn.prop("checked",false);
				};
			});
		
			this.submitBrn.click(function(){
				//alert("到结算中心");
				_this.gotoShopCart(this);
			});
		},
		gotoShopCart:function(thisLink){
			var all=this.shopCartContent.find(".shop-cart-item");
			var str=[];
				all.each(function(){
					if(!this.checked){
						str.push($(this).attr("data-id"));
					};
				});
			thisLink.href="/gotoCart.do";
					//?itemIds="+str.join(",");
			//alert(str.join(","));
		}
		
	};
	new ShopCart($("#J_ShopCart"));

});